package cn.itsource.pet.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.basic.util.SystemContext;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.service.IShopService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetDetailservice;
import cn.itsource.pet.service.IPetService;
import cn.itsource.product.domain.Product;
import cn.itsource.product.query.ProductQuery;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IShopService shopService;
    @Autowired
    private IPetDetailservice petDetailservice;
    @Autowired
    private IUserService  userService;

    /**
     * 处理未处理的寻主宠物
     * @return
     */
    @PostMapping
    public AjaxResult insertPending(@RequestBody Pet pet, HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //System.out.println(token);
            LoginInfo logininfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据logininfoid 获取user
            Shop shop = shopService.loadByLogininfoid(logininfo.getId());
            pet.setShop(shop);
            petService.insertPending(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @PatchMapping
    public PageBean<Pet>  queryPage(@RequestBody PetQuery petQuery, HttpServletRequest request){
        String token = request.getHeader("token");
        //System.out.println(token);
        LoginInfo logininfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        //根据logininfoid 获取user
        Shop shop = shopService.loadByLogininfoid(logininfo.getId());
        petQuery.setShopId(shop.getId());
        return petService.queryPage(petQuery);
    }

    /**
     * 删除宠物信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            //先删除宠物表
            petService.delete(id);
            //再删除宠物详情表
            petDetailservice.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param pets
     * @return
     */
    @PostMapping("/batchRemove")
    public  AjaxResult batchRemove(@RequestBody List<Pet> pets){
        try {
            //先删除宠物表
            petService.batchRemove(pets);
            //再删除宠物详情表
            for (Pet pet: pets) {
                petDetailservice.delete(pet.getId());
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 添加修改
     * @param
     * @return
     */
    @PutMapping
    public AjaxResult saveAndUp(@RequestBody Pet pet,HttpServletRequest request){
        try {
            //获取logininfo
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            //根据logininfoid 获取user
            Shop shop = shopService.loadByLogininfoid(loginInfo.getId());
            pet.setShop(shop);
            if(pet.getId()==null){
                petService.save(pet);
            }else {
                petService.update(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 商品上架
     * @return
     */
    @PostMapping("/onsale")
    public  AjaxResult onsale(@RequestBody List<Pet> pets){
        try {
            petService.onsale(pets);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 商品下架
     * @param
     * @return
     */
    @PostMapping("/offsale")
    public  AjaxResult offsale(@RequestBody List<Pet> pets){
        try {
            petService.offsale(pets);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    //----------------------------门户网站---------------------------------------

    /**
     * 门户网站的宠物分页查询
     * @param petQuery
     * @return
     */
    @PatchMapping("/queryHomePage")
    @ResponseBody
    public PageBean<Pet> queryHomePage(@RequestBody PetQuery petQuery){

        return petService.queryPage(petQuery);
    }

    /**
     * 门户网站宠物详情展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet getPetsById(@PathVariable Long id){
        return petService.getPetsById(id);

    }
}
